package com.omesoft.hksnore.util.data;

import android.content.Context;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Omesoft on 2017/3/13.
 */

public class UserUtil {
    /**
     * 判断语言
     *
     * @param context
     * @return
     */
    public static boolean isZh(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }

    /**
     * 定义睡眠达标时间总数
     */
    public static float sleepCountTime = 12;
    public static float nowSleepTotalTime = 0;
    public static float resultSleepTotalTime = 0;

    /**
     * 蓝牙状态
     */
    public static final int BLE_SEARCH = 80001;// 正在搜索
    public static final int BLE_CONNECTING = 80002;// 正在链接
    public static final int BLE_CONNECTED = 80003;// 已连接
    public static final int BLE_DISCONNECTED = 80004;// 断开
    public static final int BLE_BATTERY = 80005;// 电量

    public static final int BLE_SEARCHTIME1 = 10000;
    public static final int BLE_SEARCHTIME2 = 15000;

    public static boolean isDisConnected = false;

    // 把日期转为字符串
    public static String ConverToString(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return df.format(date);
    }

    // 把字符串转为日期
    public static Date ConverToDate(String strDate) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.parse(strDate);
    }

    public static int getAge(String day) throws Exception {
        Date birthDay = ConverToDate(day);
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }
        return age;
    }

    public static int getAgeByBirthday(String birthday) {
        int age = 0;
        try {
            String nowDate = getDateFormatToString(null);
            Log.d("test", "nowDate::" + nowDate);
            String[] dateStr = nowDate.split(" ");
            String[] dateNow = dateStr[0].split("-");
            String[] dateBir = birthday.split("-");
            Log.d("lgc", "dateNow[0]::" + dateNow[0]);
            Log.d("lgc", "dateBir[0]::" + dateBir[0]);
            age = Integer.valueOf(dateNow[0]) - Integer.valueOf(dateBir[0]);
            if (Integer.valueOf(dateNow[1]) < Integer.valueOf(dateBir[1])) {
                age--;
            } else if (Integer.valueOf(dateNow[1]) == Integer.valueOf(dateBir[1]) && Integer.valueOf(dateNow[2]) < Integer.valueOf(dateBir[2])) {
                age--;
            }
            // 由于系统时间不正确导致年龄为负数 。系统崩溃

        } catch (Exception e) {
            // TODO: handle exception
            age = 1;
        }
        if (age < 0) {
            age = 1;
        }
        return age;
    }

    /** 如果不传格式。。就用默认的格式:yyyy-MM-dd HH-mm-ss */
    public static String getDateFormatToString(String format) {
        Date date = new Date();
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String dateStr = dateFormat.format(date);
        return dateStr;
    }

    /**
     * 得到两日期相差几个月
     *
     * @param
     * @return
     */
    public static long getMonth(String startDate) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        long monthday;
        try {
            Date startDate1 = f.parse(startDate);
            // 开始时间与今天相比较
            Date endDate1 = new Date();

            Calendar starCal = Calendar.getInstance();
            starCal.setTime(startDate1);

            int sYear = starCal.get(Calendar.YEAR);
            int sMonth = starCal.get(Calendar.MONTH);
            int sDay = starCal.get(Calendar.DATE);

            Calendar endCal = Calendar.getInstance();
            endCal.setTime(endDate1);
            int eYear = endCal.get(Calendar.YEAR);
            int eMonth = endCal.get(Calendar.MONTH);
            int eDay = endCal.get(Calendar.DATE);

            monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

            Log.e("lgc", "monthday  " + monthday);
            Log.e("lgc", "sDay  " + sDay);
            Log.e("lgc", "eDay  " + eDay);
            if (sDay < eDay) {
                monthday = monthday + 1;
            }
            return monthday;
        } catch (ParseException e) {
            Log.e("lgc", "获取相差月数失败");
            monthday = 0;
        }
        return monthday;
    }

    /**
     * 初始化睡眠时间
     *
     * @param birthDay
     */
    public static void initSleepTime(String birthDay) {
        /**
         * 先判断年龄
         */
        int age = getAgeByBirthday(birthDay);
        if (age == 0) {
            long month = getMonth(birthDay);
            if (month <= 4) {

                Log.e("lgc", "睡眠14个小时  " + month);
                sleepCountTime = 14;
            } else if (month <= 12) {

                sleepCountTime = 12;
                Log.e("lgc", "睡眠12个小时  " + month);
            }

        } else if (age <= 2) {
            long month = getMonth(birthDay);
            Log.e("lgc", "睡眠 11个小时age<=2 " + age + "  ,m: " + month);
            sleepCountTime = 11;

        } else if (age <= 5) {
            Log.e("lgc", "睡眠10个小时 age<=5" + age);

            sleepCountTime = 10;
        } else if (age <= 13) {
            Log.e("lgc", "睡眠9个小时 age<=13" + age);

            sleepCountTime = 9;
        } else {
            Log.e("lgc", "睡眠8个小时age>13 " + age);
            sleepCountTime = 8;

        }
    }

    public static int getMonth(Date start) {
        Date end = new Date();
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);

        int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

        if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }
}
